clear textprogressbar; clear; clc;
%% Create session, Connect devices
PROJ_PATH = [current_file_path(mfilename('fullpath') ) '\'];
session=MeasurementSession.SimpleMeasurement('owner',       'ZN', ...
                                             'name',        'TestSpectrum',...
                                             'device_file', [PROJ_PATH 'device.dat'] );
                                         
session.addDescription( ...
    ['This Measurement tests the batch operation of Spectrum Module.' ...
     'Device infomation should be provide in ''device.dat''. '  ...
     'Module settings should be provide in ''settings.mat''. ' ] ...
    );
%% Spectrum Settings
zi = session.getDevice('dev3546');
spectrum = zi.spectrumModule(session);
spectrum.load_settings([PROJ_PATH 'settings.mat'], 'sptrm_settings');

%% Batch Spectrum 
session.addParameter('volt', 0.1:0.1:0.3, 'V');
session.makeLoopParameter('ScanAmp', 'volt');

result = spectrum.batchRunSession('ScanAmp', @batch_fun);
session.addMeasurementData('ScanAmp', result);

%% Finalize
session.close();
MeasurementSession.save(session);

%% Operations of Each Step
function res = batch_fun(session, module, index, varargin)
    %  get Parameters
    p=inputParser;
    isModule = @(x) isa(x, 'EquipmentControl.AnalyseEquipment.ZI.ziModule');
    isSession = @(x) isa(x, 'MeasurementSession.AbstractMeasurementSession');
    
    p.addRequired('session', isSession);    
    p.addRequired('module', isModule );
    p.addRequired('index', @isnumeric);
    p.addParameter('processMonitor', @module.plotData);
    p.parse(session, module, index, varargin{:});

    sess = p.Results.session;
    spectrum = p.Results.module;
    idx = p.Results.index;
    updataPlot = p.Results.processMonitor;
    
    zi = sess.getDevice(spectrum.device);

    fprintf('BatchRun at paramter %s(index = %d): %f ... \n', 'ScanAmp', idx, ...
            session.getLoopParameter('ScanAmp', idx) );
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %  pre_acquisition operations
    other_operations(sess, zi, idx);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % data acquisition
    spectrum.subscribeChannel('0', 'AuxIn0.fft.abs.avg');
    res = spectrum.run('index', idx, 'silence', 0, 'processMonitor', updataPlot);
    spectrum.unsubscribeChannel();  
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % post_acquisition
    pause(0.5);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

%% Other Operations
function volt = other_operations(session, zi, index)
    PROJ_PATH = [current_file_path(mfilename('fullpath') ) '\'];
    
    volt = session.getLoopParameter('ScanAmp', index);
    zi.setSigOutAmplitude('1', volt );
    zi.fireSigOut();
    
    spect = zi.spectrumModule(session);
    spect.load_settings([PROJ_PATH 'settings.mat'], 'sptrm_settings1');

    session.addParameter('freq', 100:100:200, 'Hz');
    session.makeLoopParameter('ScanFreq', 'freq');

    result = spect.batchRunSession('ScanFreq', @sub_batch_func);
    session.addMeasurementData(['ScanFreq_' num2str(index)], result);
    
    pause(1);
end

function res = sub_batch_func(session, module, index, varargin)
    %  get Parameters
    p=inputParser;
    isModule = @(x) isa(x, 'EquipmentControl.AnalyseEquipment.ZI.ziModule');
    isSession = @(x) isa(x, 'MeasurementSession.AbstractMeasurementSession');
    
    p.addRequired('session', isSession);    
    p.addRequired('module', isModule );
    p.addRequired('index', @isnumeric);
    p.addParameter('processMonitor', @module.plotData);
    p.parse(session, module, index, varargin{:});

    sess = p.Results.session;
    spectrum = p.Results.module;
    idx = p.Results.index;
    updataPlot = p.Results.processMonitor;
    
    freq = sess.getLoopParameter('ScanFreq', idx);
    fprintf('\t BatchRun at paramter %s(index = %d): %f ... \n', 'ScanFreq', idx, freq );

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %  pre_acquisition operations
    ziDAQ('setDouble', '/dev3546/oscs/0/freq', freq);
    pause(1);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % data acquisition
    spectrum.subscribeChannel('0', 'AuxIn0.fft.abs.avg');
    res = spectrum.run('index', idx, 'silence', 0, 'processMonitor', updataPlot);
    spectrum.unsubscribeChannel();  
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % post_acquisition
    ziDAQ('setDouble', '/dev3546/oscs/0/freq', 500);
    pause(0.5);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end